<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <!-- <el-col :span="12">
            <el-form-item label="父级编号" prop="parent.id"
                :rules="[
                 ]">
           			<SelectTree
                      ref="parent"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"
                      v-if="visible"
                      :url="`/mini/adv/jfMiniAdvGroup/treeData?extId=${inputForm.id}`"
                      :value="inputForm.parent.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.parent.id=value}"/>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="名称" prop="name"
                :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
			        <el-input v-model="inputForm.name" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                :rules="[
                  {required: true, message:'排序不能为空', trigger:'blur'},
                  {validator: validator.isNumber, trigger:'blur'}
                 ]">
			        <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="hasPermission('smis:system')">
            <el-form-item label="位置代码" prop="modelType"
                :rules="[
                  {required: true, message:'位置代码不能为空', trigger:'blur'}
                 ]">
			        <el-input v-model="inputForm.modelType" placeholder="请填写位置代码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="hasPermission('smis:system')">
            <el-form-item label="宽" prop="width"
                :rules="[
                  {required: true, message:'宽不能为空', trigger:'blur'}
                 ]">
			        <el-input v-model="inputForm.width" placeholder="请填写宽"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="hasPermission('smis:system')">
            <el-form-item label="高" prop="height"
                :rules="[
                  {required: true, message:'高不能为空', trigger:'blur'}
                 ]">
			        <el-input v-model="inputForm.height" placeholder="请填写高"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12" v-if="hasPermission('smis:system')">
            <el-form-item label="文件格式" prop="suffix"
                :rules="[
                  {required: true, message:'文件格式不能为空', trigger:'blur'}
                 ]">
			        <el-input v-model="inputForm.suffix" placeholder="请填写文件格式"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          parent: {
            id: ''
          },
          name: '',
          sort: '',
          modelType: '',
          width: '',
          height: '',
          suffix: ''
        }
      }
    },
    components: {
      SelectTree
    },
    methods: {
      init (method, obj) {
        this.method = method
        this.inputForm.id = obj.id
        if (method === 'add') {
          this.title = '新建广告位'
        } else if (method === 'addChild') {
          this.title = '添加下级广告位'
        } else if (method === 'edit') {
          this.title = '修改广告位'
        } else if (method === 'view') {
          this.title = '查看广告位'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.parent.id = obj.parent.id
          this.inputForm.parent.name = obj.parent.name
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/mini/adv/jfMiniAdvGroup/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfMiniAdvGroup)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/mini/adv/jfMiniAdvGroup/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshTree')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
